package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.msg.rest.service.IWxMpGroupMessageCountService;
import com.ibeeking.found.msg.rest.service.IWxMpCallBackProcessService;
import com.ibeeking.found.msg.service.common.dos.WxMpGroupMessageCountDO;
import com.ibeeking.found.msg.service.common.param.WxMpGroupMessageCountParam;
import com.ibeeking.found.msg.service.common.vo.WxMpGroupMessageCountVO;
import com.ibeeking.found.msg.service.mapper.WxMpGroupMessageCountMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.utils.bean.BeanUtil;

import com.alibaba.nacos.common.utils.IoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.chanjar.weixin.common.util.XmlUtils;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.util.xml.XStreamTransformer;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @ClassName WxMpGroupMessageCountServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-07 19:20
 **/
@Deprecated
@Service
public class WxMpGroupMessageCountServiceImpl extends ServiceImpl<WxMpGroupMessageCountMapper, WxMpGroupMessageCountDO> implements IWxMpGroupMessageCountService {

    @Resource
    private WxMpGroupMessageCountMapper wxmpGroupMessageCountMapper;



    /*@Override
    public List<WxMpGroupMessageCountVO> list(WxmpGroupMessageCountQuery wxmpGroupMessageCountQuery){
        List<WxMpGroupMessageCountDO> wxmpGroupMessageCountDOs = wxmpGroupMessageCountMapper.selectList(new LambdaQueryWrapper<WxMpGroupMessageCountDO>());
        return JsonUtils.jsonToList(wxmpGroupMessageCountDOs,WxMpGroupMessageCountVO.class);
    }

    @Override
    public Page<WxMpGroupMessageCountVO> pageList(WxmpGroupMessageCountPageQuery wxmpGroupMessageCountPageQuery){
        Page<WxMpGroupMessageCountDO> pages = wxmpGroupMessageCountMapper.selectPage(new Page<WxMpGroupMessageCountDO>(), new LambdaQueryWrapper<WxMpGroupMessageCountDO>());
        return BeanUtil.convertPage(pages , WxMpGroupMessageCountVO.class);
    }*/

    @Override
    public Boolean add(WxMpGroupMessageCountParam param) {
        WxMpGroupMessageCountDO wxmpGroupMessageCountDO = BeanUtil.convertBean(param, WxMpGroupMessageCountDO.class);
        return wxmpGroupMessageCountMapper.insert(wxmpGroupMessageCountDO) > 0;
    }

    /**
     * 新增
     *
     * @param countDO
     * @return
     */
    @Override
    public Boolean add(WxMpGroupMessageCountDO countDO) {
        Long userId  = UserUtils.getUserId();
        countDO.setCreateBy(userId);
        countDO.setCreateTime(LocalDateTime.now());
        countDO.setModifyTime(LocalDateTime.now());
        return wxmpGroupMessageCountMapper.insert(countDO) > 0;
    }

    @Override
    public Boolean modify(WxMpGroupMessageCountParam param) {
        Long userId  = UserUtils.getUserId();
        WxMpGroupMessageCountDO wxmpGroupMessageCountDO = BeanUtil.convertBean(param, WxMpGroupMessageCountDO.class);
        wxmpGroupMessageCountDO.setModifyBy(userId);
        wxmpGroupMessageCountDO.setModifyTime(LocalDateTime.now());
        return wxmpGroupMessageCountMapper.updateById(wxmpGroupMessageCountDO) > 0;
    }

    @Override
    public WxMpGroupMessageCountVO view(Long id) {
        WxMpGroupMessageCountDO wxmpGroupMessageCountDO = wxmpGroupMessageCountMapper.selectById(id);
        return BeanUtil.convertBean(wxmpGroupMessageCountDO, WxMpGroupMessageCountVO.class);
    }

    @Override
    public Boolean changeStatus(Long id, Integer status) {
        WxMpGroupMessageCountDO wxmpGroupMessageCountDO = new WxMpGroupMessageCountDO();
        wxmpGroupMessageCountDO.setId(id);
        wxmpGroupMessageCountDO.setModifyTime(LocalDateTime.now());
        return wxmpGroupMessageCountMapper.updateById(wxmpGroupMessageCountDO) > 0;
    }

    @Override
    public Boolean del(Long id) {
        return wxmpGroupMessageCountMapper.deleteById(id) > 0;
    }

    /**
     * 预留公众号群发任务后回调接口
     *
     * @param file
     */
    @Override
    public String groupMessageCallback(MultipartFile file) {
        final String encode = "UTF-8";
        Map<String, Object> map = null;
        try {
            map = XmlUtils.xml2Map(IoUtils.toString(file.getInputStream(), encode));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String msgID = String.valueOf(map.get("MsgID"));
        QueryWrapper<WxMpGroupMessageCountDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WxMpGroupMessageCountDO::getMsgId, msgID);
        WxMpGroupMessageCountDO wxmpGroupMessageCountDO = new WxMpGroupMessageCountDO();
        wxmpGroupMessageCountDO.setFailedCount(Integer.valueOf(String.valueOf(map.get("ErrorCount"))));
        wxmpGroupMessageCountDO.setSendCount(Integer.valueOf(String.valueOf(map.get("TotalCount"))));
        wxmpGroupMessageCountDO.setSendStatus(String.valueOf(map.get("Status")));
        wxmpGroupMessageCountDO.setFilterCount(Integer.valueOf(String.valueOf(map.get("FilterCount"))));
        wxmpGroupMessageCountDO.setSuccessCount(Integer.valueOf(String.valueOf(map.get("SentCount"))));
        this.wxmpGroupMessageCountMapper.update(wxmpGroupMessageCountDO, queryWrapper);
        return "success";
    }

    /**
     * @param file
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String groupMessageCallback(String appId, MultipartFile file) {

        WxMpXmlMessage baseMassage = null;
        IWxMpCallBackProcessService service = null;
        InputStream in = null;
        try {
            in = file.getInputStream();
            baseMassage = XStreamTransformer.fromXml(WxMpXmlMessage.class, in);
            service = SpringBeanUtil.getBean(baseMassage.getMsgType(), IWxMpCallBackProcessService.class);
        } catch (NoSuchBeanDefinitionException | IOException e) {
            service = SpringBeanUtil.getBean("default", IWxMpCallBackProcessService.class);
        }
        return service.process(baseMassage, appId);
    }


}
